﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TheoriesBraun.DAL;
using System.Data.SqlClient;

namespace TheoriesBraun
{
    public partial class Theories : Form
    {
        Login loginForm = new Login();
        SqlDataAdapter adapter;
        DataTable dt;
        BindingSource bs;

        public Theories()
        {
            InitializeComponent();
            loadMenuTheories();
        }

        private void Theories_Load(object sender, EventArgs e)
        {
            adapter = new SqlDataAdapter("Select * from Theories", DBConnect.getConnection());
            dt = new DataTable("Theories");
            adapter.Fill(dt);
            bs = new BindingSource();

            foreach (DataRow data in dt.Rows)
                bs.Add(data);

            ShowRecord();
        }

        private void ShowRecord()
        {
            DataRow currentRow = (DataRow)bs.Current;
            lblChapter.Text = currentRow["Chapter"].ToString();
            lblTitle.Text = currentRow["NameOfChapter"].ToString();
            txtContents.Text = currentRow["Description"].ToString();
        }

        private void loginToolStripMenuItem_Click(object sender, EventArgs e)
        {
            loginForm.Show();
            this.Hide();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            bs.MoveNext();
            ShowRecord();
        }

        private void btnPreview_Click(object sender, EventArgs e)
        {
            bs.MovePrevious();
            ShowRecord();
        }

        private void btnHome_Click(object sender, EventArgs e)
        {
            theoryPanel.Visible = false;
            panelMenu.Visible = true;
        }

        private void btnBegin_Click(object sender, EventArgs e)
        {
            btnBegin.Visible = false;
            panelMenu.Visible = true;
        }

        public void loadMenuTheories()
        {
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter("SELECT (Chapter + ' - ' + NameOfChapter) as ChapterAndName FROM Theories", DBConnect.getConnection());
            da.Fill(ds, "Theories");
            foreach (DataRow dr in ds.Tables["Theories"].Rows)
            {
                TreeNode tn = new TreeNode(dr["ChapterAndName"].ToString());
                tvMenu.Nodes.Add(tn);
            }
        }

        private void tvMenu_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            string text = e.Node.Text;
            string[] chapter = text.Split('-');
            panelMenu.Visible = false;
            theoryPanel.Visible = true;
            //MessageBox.Show(chapter[0]);
        }
    }
}
